package com.jiancong.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiancong.domain.ResponseResult;
import com.jiancong.domain.entity.Tag;
import com.jiancong.domain.vo.PageVo;
import com.jiancong.domain.vo.TagListDto;
import com.jiancong.domain.vo.TagVo;
import com.jiancong.enums.AppHttpCodeEnum;
import com.jiancong.exception.SystemException;
import com.jiancong.mapper.TagMapper;
import com.jiancong.service.TagService;
import com.jiancong.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * 标签(Tag)表服务实现类
 *
 * @author makejava
 * @since 2024-05-09 19:20:49
 */
@Service
public class TagServiceImpl implements TagService {

    @Autowired
    private TagMapper tagMapper;
    @Override
    public ResponseResult<PageVo> pageTagList(Integer pageNum, Integer pageSize, TagListDto tagListDto) {
        PageHelper.startPage(pageNum,pageSize);
        //把记录所有数据的集合转化为Page类型
        PageInfo<TagVo> pageInfo = new PageInfo<>(tagMapper.TagList(tagListDto));
        List<TagVo> tagVos = pageInfo.getList();
        PageVo pageVo = new PageVo(tagVos,pageInfo.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    @Transactional
    public ResponseResult addTag(Tag tag) {      //标签不能为空
        if(!StringUtils.hasText(tag.getName()) || !StringUtils.hasText(tag.getRemark())){
            throw new SystemException(AppHttpCodeEnum.LABEL_CANNOT);
        }
        Long userId = SecurityUtils.getUserId();
        tag.setCreateBy(userId);
        tag.setUpdateBy(userId);
        tag.setCreateTime(new Date());
        tag.setUpdateTime(new Date());
        tagMapper.addTag(tag);

        return ResponseResult.okResult();
    }

    @Override
    @Transactional
    public ResponseResult deleteTag(List<Long> id) {
        for (int i = 0; i < id.size(); i++) {
            Tag tag = new Tag();
            Long userId = SecurityUtils.getUserId();
            tag.setId(id.get(i));
            tag.setCreateBy(userId);
            tag.setUpdateTime(new Date());
            tagMapper.deleteTag(tag);
        }
        return ResponseResult.okResult();
    }

    @Override
    @Transactional
    public ResponseResult updateTag(Tag tag) {
        
        if(!StringUtils.hasText(tag.getName()) || !StringUtils.hasText(tag.getRemark())){
            throw new SystemException(AppHttpCodeEnum.LABEL_CANNOT);
        }
        Long userId = SecurityUtils.getUserId();
        tag.setUpdateBy(userId);
        tag.setUpdateTime(new Date());
        tagMapper.updateTag(tag);

        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getTag(Long id) {
        TagVo tagVo = tagMapper.getTag(id);
        return ResponseResult.okResult(tagVo);
    }

    @Override
    public List<TagVo> listAllTag() {
        List<TagVo> tagVos = tagMapper.listAllTag();
        return tagVos;
    }
}
